package model;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JTextPane;

import model.BinSetVariables;

/**
 * 
 * @author Estelle && Marie
 * 
 * This class is to get the contents of a list which storages in our project
 *
 */

public class FileList {
	
	/**
	 *  We write in the file to change all the contents of a file
	 *  We get the name of the list and the bin used for the form in parameters of the function to set this last one
	 * @param name
	 * @param bin
	 *
	 * @return bin 
	 */
	
	public void writeFile(String name, BinSetVariables bin){
		try
		{
		    FileWriter fw = new FileWriter (name);
		    
		    	fw.write(bin.getCallPutFlag());
		    	 fw.write ("\r\n");
		    	
		    	fw.write(String.valueOf(bin.getS()));
		    	 fw.write ("\r\n");
		    	
		    	fw.write(String.valueOf(bin.getX()));
		    	 fw.write ("\r\n");
		    	
		    	fw.write(String.valueOf(bin.getT()));
		    	 fw.write ("\r\n");
		    	
		    	fw.write(String.valueOf(bin.getV()));
		    	 fw.write ("\r\n");
		    
		    	fw.write(String.valueOf(bin.getR()));
		    	 fw.write ("\r\n");
		    	
		    	fw.write(String.valueOf(bin.getnSteps()));
		    	 fw.write ("\r\n");
		    	 
		    	fw.write(String.valueOf(bin.getnSimulations()));
		    	 fw.write ("\r\n");

		    fw.close();
		}
		catch (IOException exception)
		{
		    System.out.println("Erreur lors de la lecture : " + exception.getMessage());
		}
	}
	
	/**
	 * To read a list for get it contents to fill the form in our mainWindow
	 * @param name
	 * @param text
	 * @return
	 */
	public BinSetVariables readFile(String name, JTextPane text){
		BinSetVariables bin = new BinSetVariables();
		int i =0;
		
		try
		{
		    File f = new File (name);
		    FileReader fr = new FileReader (f);
		    BufferedReader br = new BufferedReader (fr);
		    
		    try
		    {
		        String line = br.readLine();
		/**
		 *  Each line is a value of a binSetVariable that's why we decided to use a switch to get each value of a list.
		 *  For this fact, we increment a integer i to know which one of the bin's variables storage in the String line.
		 * 
		 */
		        while (line != null)
		        {	
		        	
		        		switch(i){
		            	case 0:
		            	
		            		if(line != null) {
		            			bin.setCallPutFlag(line);
		            			i++;
		            		}
		            	break;
		            	case 1:
		            		
		            		if(line != null) {
		            			bin.setS(Double.valueOf(line));
		            			i++;
		            		}
		            	break;
		            	case 2:
		            		
		            		if(line != null) {
		            			bin.setX(Double.valueOf(line));
		            			i++;
		            		}
		            	break;
		            	case 3:
		            		
		            		if(line != null) {
		            			bin.setT(Double.valueOf(line));
		            			i++;
		            		}
		            	break;
		            	case 4:
		            		
		            		if(line != null) {
		            			bin.setR(Double.valueOf(line));
		            			i++;
		            		}
		            	break;
		            	case 5:
		            		
		            		if(line != null) {
		            			bin.setB(Double.valueOf(line));
		            			i++;
		            		}
		            	break;
		            	case 6:
		            		
		            		if(line != null) {
		            			bin.setV(Double.valueOf(line));
		            			i++;
		            		}
		            	break;
		            	case 7:
		            		
		            		if(line != null) {
		            			bin.setnSteps(Integer.valueOf(line));
		            			i++;
		            		}
		            	break;
		            	case 8:
		            		
		            		if(line != null) {
		            			bin.setnSimulations(Integer.valueOf(line));
		            			i=0;
		            		}
		            	break;	
		            	
		            }
		            
		            line = br.readLine();
		            
		        }
		        
		        br.close();
		        fr.close();
		    }
		    catch (IOException exception)
		    {
		    	text.setText("ERROR of Reading in controller.FileList : " + exception.getMessage());
		    }
		}
		catch (FileNotFoundException exception)
		{
		    text.setText("ERROR in controller.FileList : the file called " + name + " was not found");
		}
		
		return bin;
	}
}
